package com.example.demo.controller;

import com.example.demo.dao.StudentDao;
import com.example.demo.pojo.Student;
import com.example.demo.pojo.dto.Borrow;
import com.example.demo.pojo.vo.StudentBorrowVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;

/**
 * @author chenkaiqi
 */
@RestController
@CrossOrigin(origins = "*")
@Api(value = "productController",tags = "学生接口")
public class StudentController {
    final StudentDao stuDao;

    private StudentController(StudentDao dao){
        this.stuDao = dao;
    }
    @RequestMapping(path="/student-login", method = RequestMethod.POST)
    public HttpStatus login(@RequestBody Map<String, String> paras){
        final Student stu;
        stu  = stuDao.findStudentByIdAndPassword(Integer.parseInt(paras.get("id")),
                                            paras.get("password"));
        // 数据库查询不到 返回错误信息
        if(stu == null) {
            return HttpStatus.BAD_REQUEST;
        }
        return HttpStatus.OK;
    }

    /**
     * 借书的Api 将借书记录插入数据库
     * @param borrow
     * @return
     */
    @RequestMapping(path="/borrow-book", method = RequestMethod.POST)
    public HttpStatus borrow(@RequestBody Borrow borrow) {
//        借阅期限30天，自然日
        int i = 0;
        i = stuDao.insertBorrowed(borrow);
        if(i > 0){
            return HttpStatus.OK;
        }
        return HttpStatus.BAD_REQUEST;
    }

    /**
     * 查看
     * @return
     */
    @ApiOperation(value ="查看借阅")
    @RequestMapping(path="/borrows", method = RequestMethod.GET)
    public List<StudentBorrowVo> borrowVo(
            @RequestParam("stuId") Integer stuId,
            @RequestParam(defaultValue = "5") Integer pageSize,
            @RequestParam(defaultValue = "1") Integer pageNum
    ) {
        List borrowVoList = new ArrayList();

        Date now = new Date();
        LocalDateTime ldt = now.toInstant()
                .atZone(ZoneId.systemDefault())
                .toLocalDateTime();
        // 将 localDateTime月份加一后转化为Date对象
        Date backDay = Date.from(ldt.plusMonths(1)
                .atZone(ZoneId.systemDefault())
                .toInstant());

//       List borrowVoList = stuDao.findBorrowBooks()
        // 表查询 Vo list 放入 后返回
        StudentBorrowVo studentBorrowVo1 = new StudentBorrowVo();
        StudentBorrowVo studentBorrowVo2 = new StudentBorrowVo();
        studentBorrowVo1.setAuthor("author");
        studentBorrowVo2.setAuthor("author");
        studentBorrowVo1.setBackDate(backDay);
        studentBorrowVo1.setBorrowDate(now);
        //  做判断 当前时间 和借书日期 比较一下 直接调用 还书的api


        studentBorrowVo2.setBorrowDate(now);
        borrowVoList.add(0,studentBorrowVo1);
        borrowVoList.add(1,studentBorrowVo2);
        return borrowVoList;
    }

    //    @RequestMapping(path = "/{stuId}/borrows")
//    public

}
